<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="门票价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="门票价格"></el-input>
    </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.sightId" style="float:left" filterable placeholder="选择景区">
          <el-option v-for="(item, index) in sightOption" :key="index" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="票种类" prop="name">
      <el-input v-model="dataForm.name" placeholder="请输入名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        sightId: '',
        sightOption: [],
        visible: false,
        dataForm: {
          id: 0,
          price: '',
          sightId: '',
          name: ''
        },
        dataRule: {
          price: [
            { required: true, message: '门票价格不能为空', trigger: 'blur' }
          ],
          sightId: [
            { required: true, message: '景点id不能为空', trigger: 'blur' }
          ],
          ageStatus: [
            { required: true, message: '0 半票 1全票不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.getSightSelectList();
    },

    methods: {
      //获取景点下拉框
      getSightSelectList() {
        this.$http.get("/sights/list/select").then(res => {
          // console.log(res.data)
          this.sightOption = res.data.data;
        });
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (id) {
            this.$http.get("/ticket/detail/"+id).then(res => {
              // console.log(res.data.data)
              this.dataForm = res.data.data
            });
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http.post("/ticket/addOrUpdate", this.dataForm).then(res => {
              console.log(res.data)
              if (res.data.status) {
                this.$message({
                  message: res.data.data,
                  type: 'success',
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                });

                this.form = {}
                this.searchByNameFun()
              } else {
                this.form = {}
                this.$message.error(res.data.msg);
              }
            })
          }
        })
      }
    }
  }
</script>
